<template>
  <view class="container">
    <template v-for="item in list">
      <uni-list-item :title="item.name" v-if="!item.hidden">
        <template #footer>
          <switch
            style="transform: scale(0.7)"
            :checked="item.value"
            @change="item.value = !item.value"
            color="#00C853"
          />
        </template>
      </uni-list-item>
    </template>
  </view>
  <view style="height: 30rpx"></view>
  <view style="text-align: center">
    <text class="btn" @click="save">保存</text></view
  >
  <view style="height: 60rpx"></view>
</template>
<script setup>
import { reactive, ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";

import { setLineAlarm } from "@/api/wuLian.js";
const options = ref({});
onLoad((option) => {
  options.value = option;
  if (option.enableAlarm) {
    for (let i = 0; i < list.length; i++) {
      if (option.enableAlarm[i] == 1) {
        list[i].value = true;
      } else {
        list[i].value = false;
      }
    }
  }
});
const list = reactive([
  {
    name: "短路报警",
    hidden: false,
    value: false,
  },
  {
    name: "浪涌报警",
    hidden: false,
    value: false,
  },
  {
    name: "过载报警",
    hidden: false,
    value: false,
  },
  {
    name: "温度预警",
    hidden: false,
    value: false,
  },
  {
    name: "漏电报警",
    hidden: false,
    value: false,
  },
  {
    name: "过流报警",
    hidden: false,
    value: false,
  },
  {
    name: "过压报警",
    hidden: false,
    value: false,
  },
  {
    name: "bit7",
    hidden: true,
    value: false,
  },
  {
    name: "bit8",
    hidden: true,
    value: false,
  },
  {
    name: "输入缺相(3-4P 开关)",
    hidden: false,
    value: false,
  },
  {
    name: "电弧报警",
    hidden: false,
    value: false,
  },
  {
    name: "欠压报警",
    hidden: false,
    value: false,
  },
  {
    name: "过压预警",
    hidden: false,
    value: false,
  },
  {
    name: "欠压预警",
    hidden: false,
    value: false,
  },
  {
    name: "漏电预警",
    hidden: false,
    value: false,
  },
  {
    name: "电流预警",
    hidden: false,
    value: false,
  },
  {
    name: "停电以后，再次通电后，开关自动分闸功能",
    hidden: false,
    value: false,
  },
  {
    name: "允许本地检修模式",
    hidden: false,
    value: false,
  },
  {
    name: "恶性负载报警(1P开关)",
    hidden: false,
    value: false,
  },
  {
    name: "允许远程锁定模式",
    hidden: false,
    value: false,
  },
  {
    name: "bit20",
    hidden: true,
    value: false,
  },
  {
    name: "温度报警",
    hidden: false,
    value: false,
  },
  {
    name: "三相不平衡(3-4P 开关)",
    hidden: false,
    value: false,
  },
  {
    name: "相序错误(3-4P 开关)",
    hidden: false,
    value: false,
  },
  {
    name: "bit24",
    hidden: true,
    value: false,
  },
  {
    name: "输出缺相(3-4P 开关)",
    hidden: false,
    value: false,
  },
  {
    name: "bit26",
    hidden: true,
    value: false,
  },
  {
    name: "bit27",
    hidden: true,
    value: false,
  },
  {
    name: "bit28",
    hidden: true,
    value: false,
  },
  {
    name: "bit29",
    hidden: true,
    value: false,
  },
  {
    name: "bit30",
    hidden: true,
    value: false,
  },
  {
    name: "bit31",
    hidden: true,
    value: false,
  },
]);
function setAlarmConfig(type, value) {
  return new Promise((resolve, reject) => {
    setLineAlarm({
      mac: options.value.mac,
      value1: options.value.addr,
      value2: type,
      value3: value,
    }).then((res) => {
      if (res.code == 0) {
        uni.showToast({
          title: res.msg,
          icon: "none",
        });
      } else {
        uni.showToast({
          icon: "none",
          title: res.msg,
        });
      }
      resolve(res);
    });
  });
}

function save() {
  let temp = [];
  for (let i = 0; i < list.length; i++) {
    if (list[i].value) {
      temp.push(1);
    } else {
      temp.push(0);
    }
  }
  setAlarmConfig("enableAlarm", temp.join("")).then((e) => {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  });
}
</script>
<style scoped>
.btn {
  padding: 20rpx 140rpx;
  font-size: 26rpx;
  color: #fff;
  background-color: #e60012;
}
:deep(.uni-list-item__content) {
  justify-content: center !important;
}
</style>
